/* $Id: ProblemEntry.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import alt.djudge.frontend.server.utils.Parsers;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;
import alt.djudge.frontend.shared.dto.admin.ProblemEntryDTO;


@PersistenceCapable(detachable="true")
public class ProblemEntry extends AbstractEntry
{
	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private String sid = "-";

	@Persistent
	private String name = "-";

	@Persistent
	private Integer timeLimit = 1;
	
	@Persistent
	private Integer memoryLimit = 1;
	
	@Persistent
	private Integer outputLimit = 1;
	
	@Persistent
	private String djudgeContest = "-";
	
	@Persistent
	private String djudgeProblem = "-";

	@Persistent
	private String statement = new String("-");
	
	public ProblemEntry()
	{
		// TODO Auto-generated constructor stub
	}
	
	public ProblemEntry(Element next)
	{
		readXml(next);
	}

	public String getSid()
	{
		return sid;
	}

	public void setSid(String sid)
	{
		this.sid = sid;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getTimeLimit()
	{
		return timeLimit;
	}

	public void setTimeLimit(int timeLimit)
	{
		this.timeLimit = timeLimit;
	}

	public int getMemoryLimit()
	{
		return memoryLimit;
	}

	public void setMemoryLimit(int memoryLimit)
	{
		this.memoryLimit = memoryLimit;
	}

	public String getStatement()
	{
		return statement;
	}

	public void setStatement(String statement)
	{
		this.statement = statement;
	}

	public String getDjudgeContest()
	{
		return djudgeContest;
	}

	public void setDjudgeContest(String djudgeContest)
	{
		this.djudgeContest = djudgeContest;
	}

	public String getDjudgeProblem()
	{
		return djudgeProblem;
	}

	public void setDjudgeProblem(String djudgeProblem)
	{
		this.djudgeProblem = djudgeProblem;
	}

	@Override
	public String toString()
	{
		return sid + ": " + name;
	}

	public Document getXml()
	{
		Document doc = DocumentHelper.createDocument();
		Element elem = doc.addElement("ContestProblem");
		
		elem.addAttribute("id", "" + id);
		elem.addAttribute("sid", "" + sid);
		elem.addAttribute("name", "" + name);
		elem.addAttribute("active", "" + active);
		elem.addAttribute("timeLimit", "" + timeLimit);
		elem.addAttribute("memoryLimit", "" + memoryLimit);
		elem.addAttribute("djudgeContest", "" + djudgeContest);
		elem.addAttribute("djudgeProblem", "" + djudgeProblem);
				
		return doc;
	}
	
	private void readXml(Element el)
	{
		sid = el.attributeValue("sid");
		name = el.attributeValue("name");
		active = Parsers.parseBoolean(el.attributeValue("active"), true);
		timeLimit = Parsers.parseInteger(el.attributeValue("timeLimit"), 2000);
		memoryLimit = Parsers.parseInteger(el.attributeValue("memoryLimit"), 67108864);
		djudgeProblem = el.attributeValue("djudgeProblem");
		djudgeContest = el.attributeValue("djudgeContest");
	}

	public void setOutputLimit(Integer outputLimit)
	{
		this.outputLimit = outputLimit;
	}

	public Integer getOutputLimit()
	{
		return outputLimit;
	}
	
	@Override
	public void fromDTO(AdminDTOEntry dtoEntry)
	{
		setSid(dtoEntry.getFieldValue(1));
		setName(dtoEntry.getFieldValue(2));
		setActive(Boolean.parseBoolean(dtoEntry.getFieldValue(3)));
		setTimeLimit(Integer.parseInt(dtoEntry.getFieldValue(4)));
		setMemoryLimit(Integer.parseInt(dtoEntry.getFieldValue(5)));
		setDjudgeContest(dtoEntry.getFieldValue(6));
		setDjudgeProblem(dtoEntry.getFieldValue(7));
	}

	@Override
	public AdminDTOEntry ownAdminDTO()
	{
		String[] data = new String[] {
			getId().toString(),
			getSid(),
			getName(),
			"" + getTimeLimit(),
			"" + getMemoryLimit(),
			getDjudgeContest(),
			getDjudgeProblem(),
		};
		return new ProblemEntryDTO(data);
	}
}
